import urllib.request

from bs4 import BeautifulSoup
from django.core.management.base import BaseCommand

from content_center.models import UniversityLabel


class Command(BaseCommand):
    def downloadDxmd(self):
        site = "http://www.chinadegrees.cn/xwyyjsjyxx/xwsytjxx/qgptgxmd/qgptgxmd.html"
        html = urllib.request.urlopen(site)
        soup = BeautifulSoup(html, "html.parser")
        labels = []
        for id, tr in enumerate(soup.find_all("tr")):
            if id != 0:
                tds = tr.find_all("td")
                if len(tds) == 5:
                    labels.append(
                        UniversityLabel(
                            university_name=tds[1].contents[0],
                            competent_department=tds[2].contents[0],
                            location=tds[3].contents[0],
                            running_level=tds[4].contents[0],
                        )
                    )
        UniversityLabel.objects.bulk_create(labels)

    def handle(self, *args, **options):
        self.downloadDxmd()
